<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class OAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod {
  public function get_name() {
    return "RSA-SHA1";
  }

  protected function fetch_public_cert(&$request) {
                            throw Exception("fetch_public_cert not implemented");
  }

  protected function fetch_private_cert(&$request) {
                    throw Exception("fetch_private_cert not implemented");
  }

  public function build_signature(&$request, $consumer, $token) {
    $base_string = $request->get_signature_base_string();
    $request->base_string = $base_string;

        $cert = $this->fetch_private_cert($request);

        $privatekeyid = openssl_get_privatekey($cert);

        $ok = openssl_sign($base_string, $signature, $privatekeyid);

        openssl_free_key($privatekeyid);

    return base64_encode($signature);
  }

  public function check_signature(&$request, $consumer, $token, $signature) {
    $decoded_sig = base64_decode($signature);

    $base_string = $request->get_signature_base_string();

        $cert = $this->fetch_public_cert($request);

        $publickeyid = openssl_get_publickey($cert);

        $ok = openssl_verify($base_string, $decoded_sig, $publickeyid);

        openssl_free_key($publickeyid);

    return $ok == 1;
  }
}